/**
 * axios基础构建
 * @date 2019-12-24
 */
import { message, Modal } from 'antd';
import instance from './axios.intercept';
import { AxiosRequest, CustomResponse } from './axios.types';



class Abstract {
    // 外部传入的baseUrl
    protected baseURL: string = "";
    // 自定义header头
    protected headers: object = {
        ContentType: 'application/json;charset=UTF-8'
    }

    private apiAxios({ baseURL = this.baseURL, headers = this.headers, method, url, data, params, responseType }: AxiosRequest): Promise<CustomResponse> {

        return new Promise((resolve, reject) => {
            instance({
                baseURL,
                headers,
                method,
                url,
                params,
                data,
                responseType
            }).then((res) => {
                // 200:服务端业务处理正常结束
                if (res.data.code === 200) {
                    resolve({ status: true, message: 'success', data: res.data, origin: res.data });
                } else if (res.data.code === 401) {
                    Modal.info({
                        title: "提示",
                        content: "您未登录，请先尼玛登录去！！！",
                        onOk: () => {
                            window.location.href = window.location.origin + '/login'
                            // history.push("/login")
                        }
                    })
                } else {
                    message.error(res.data?.message);

                    resolve({ status: false, message: res.data?.message || (url + '请求失败'), data: res.data });
                }
            }).catch((err) => {
                console.log(err)
                const message = err?.data.message || (url + '请求失败');
                // message.error(message);
                Modal.info({
                    title: "提示",
                    content: message,
                    onOk: () => {

                    }
                })
                reject({ status: false, message, data: null });
            });
        });
    }

    /**
     * GET类型的网络请求
     */
    protected getReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
        return this.apiAxios({ baseURL, headers, method: 'GET', url, data, params, responseType });
    }

    /**
     * POST类型的网络请求
     */
    protected postReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
        return this.apiAxios({ baseURL, headers, method: 'POST', url, data, params, responseType });
    }

    /**
     * PUT类型的网络请求
     */
    protected putReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
        return this.apiAxios({ baseURL, headers, method: 'PUT', url, data, params, responseType });
    }

    /**
     * DELETE类型的网络请求
     */
    protected deleteReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
        return this.apiAxios({ baseURL, headers, method: 'DELETE', url, data, params, responseType });
    }
}

export default Abstract;
